package com.she.iiicoursecenter.view.tab4;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab4_message.Tab4_MainActivity;
import com.she.iiicoursecenter.activity.tab4_message.Tab4_MessageDetail;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.connect.bean.GetMsgBean;
import com.she.iiicoursecenter.manager.AsyncImageLoader;
import com.she.iiicoursecenter.manager.AsyncImageLoader.ImageCallback1;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab4_MessageDetailView extends CustomizedView implements OnClickListener {

	// test only!!!
	private ImageView mImgBack;
	private AsyncImageLoader asyncImageLoader;
	
	public Tab4_MessageDetailView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		//
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab4_messagedetail, this, true);
		asyncImageLoader = new AsyncImageLoader();
		//

		// test only!!!
		mImgBack = (ImageView) this.findViewById(R.id.img_back);
		mImgBack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = Tab4_MessageDetailView.this.getApp().mChangeViewHandler.obtainMessage();
				msg.what = Tab4_MessageDetailView.this.getApp().MSG_WHAT_CHANGEVIEW;
				ViewChageDataBean dataBean = new ViewChageDataBean(Tab4_MessageDetail.class, Tab4_MainActivity.class, null);
				msg.obj = dataBean;
				Tab4_MessageDetailView.this.getApp().mChangeViewHandler.sendMessage(msg);
				getApp().fadeout = true;
			}
		});
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void notifyView(int postiton){
		GetMsgBean msg = getApp().getMsgData().get(postiton);
		((TextView)this.findViewById(R.id.tv_title)).setText(msg.getName());
		if (msg.getName().length()>=8) {
			((TextView)this.findViewById(R.id.tv_title)).setTextSize(18);
		}
		if (msg.getName().length()>=10) {
			((TextView)this.findViewById(R.id.tv_title)).setTextSize(18);
			((TextView)this.findViewById(R.id.tv_title)).setText(msg.getName().substring(0, 10)+"...");
		}
		((TextView)this.findViewById(R.id.tab4_detail_content)).setText(msg.getContent());
		
		asyncImageLoader.loadDrawable(getContext() , msg.getPic() ,new ImageCallback1() {
			
			public void imageLoaded(Drawable imageDrawable, String imageUrl) {
				// TODO Auto-generated method stub
//				if (imageDrawable == null) {
//					((ImageView)Tab4_MessageDetailView.this.findViewById(R.id.tab4_detail_img)).setVisibility(View.GONE);
//				}else{
					((ImageView)Tab4_MessageDetailView.this.findViewById(R.id.tab4_detail_img)).setImageDrawable(imageDrawable);
//				}
			}
		});
	}
}
